package com.catsic.pub.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.form.DrawerForm;

@Controller
@RequestMapping("/drawer")
public class DrawerController extends BaseController {
	
	@Autowired
	private DrawerService drawerService;
	
	/**
	 * create:添加
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/create" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> create(DrawerForm form){
		form = drawerService.add(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * update:更新
	 * @author yangyd
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "/update" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update(DrawerForm form){
		form = drawerService.update(form);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * delete:删除
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		drawerService.delete(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
	 * recover:恢复
	 *
	 */
	@RequestMapping(value = "/recover")
	@ResponseBody
	public Object recover(String id){
		drawerService.recover(id);
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		map.put("success", true);
		return map;
	}
    /**
     * active:激活
	 * @author yangyd
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/active")
	@ResponseBody
	public Map<String,Object> active(String id){
		drawerService.active(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过父节点ID获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/query" )
	@ResponseBody
	public Map<String,Object> query(String id){
		List<DrawerForm> list = drawerService.query(id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过父节点编码获取子节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/findByParentCode" )
	@ResponseBody
	public Map<String,Object> findByParentCode(String pcode){
		List<DrawerForm> list = drawerService.findByParentCode(pcode);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, list);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	/**
     * 通过节点编码获取节点
	 * @author yangyd
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/findByCode" )
	@ResponseBody
	public Map<String,Object> findByCode(String code){
		DrawerForm form = drawerService.findDrawerByCode(code);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
}
